const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({
  data: {
    isfixed:false,
    listsheight:20,
    cate:'应用案例',
    iscate:false,
    categoryList:[],
    productList:[]

  },
  onLoad() {
    this.loadData();
  },
  onShow(){
 
  },
  selectcate(e){
    let _this = this
    console.log(e.currentTarget.dataset)
    _this.setData({
      cate:e.currentTarget.dataset.name,
      // index:e.currentTarget.dataset.index,
      iscate:false,
    })
    let typeid = e.currentTarget.dataset.id
    this.loadData(typeid)
  },
  opencate(){
    this.setData({
      iscate:!this.data.iscate,
    })
  },
  closecate(){
    this.setData({
      cate:'',
      iscate:false,
    })
  },
  onPageScroll(event) {

    let scrollTop = event.scrollTop //页面滚动
    if (scrollTop >= this.data.listsheight) { //页面向上滚动indexKey
      this.setData({
        isfixed:true
      })
    } else { //页面向下滚动
      this.setData({
        isfixed:false
      })
    }
  },
  onShareAppMessage: function (e) {
    
  },
  onShareTimeline: function(res){
    
  },
  loadData: function(typeid = null){
    let _this = this
    Controller.case({
      "apiChannel": "ekey=1&type=sonself&dir_name=hi_chanpin",
      "apiList": `ekey=1&page=1&typeid=${typeid}`,
      "channel_alias": "product"
    }).then(res => {
      console.log(res.data)
      let categoryList = res.data.apiChannel[1],
      productList = res.data.apiList[1]
      console.log(categoryList)
      _this.setData({
        categoryList,
        productList
      });
      console.log(productList)
    }).catch(res=>{
    })
  },


    /**
   * 跳转列表页
   */
  jumpView(e) {
    util.jumpList(e)
  },

  collectBtn(e){
    let _this = this
    let aid = e.currentTarget.dataset.id
    Controller.collect({
      "aid": aid,
    }).then(res => {
      console.log( 'aaaa',aid,this.data.productList)
      console.log(res.data.is_collect)
      this.data.productList.data.forEach( (element ,index)=> {
        if(element.aid == aid){
          this.data.productList.data[index].is_collect= res.data.is_collect
        }
      });
      let productList = this.data.productList
      console.log('aabbbb',productList)
      _this.setData({
        productList:this.data.productList
      });
    }).catch(res=>{
  
    })
  },
  goto(e){
    let url = e.currentTarget.dataset.url
    Controller.foot({
      "aid": e.currentTarget.dataset.id,
    }).then(res => {
    }).catch(res=>{
    })
    wx.navigateTo({
      url: `/pages/webview/webview?url=${url}`,
    })
  }
})